// import 'package:filepicker_windows/filepicker_windows.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:shimo_app/pages/components/baseDialog.dart';
import 'package:shimo_app/pages/components/hint.dart';
import 'package:shimo_app/pages/index_page/index_controller.dart';
import 'package:shimo_app/pages/components/confirmdDialog.dart';

import '../../services/device_service.dart';
import '../components/loading_button.dart';
import '../components/number_input.dart';
import 'controller.dart';

class SettingComponentComponent extends StatelessWidget {
  SettingComponentComponent({Key? key}) : super(key: key);

  final controller = Get.put(SettingComponentController());
  final state = Get.find<SettingComponentController>().state;

  final tabs = ["设备设定", "参数设定", "用户管理"];

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SettingComponentController>(builder: (controller) {
      return SizedBox(
        height: MediaQuery.of(context).size.height -
            MediaQuery.of(context).padding.top -
            MediaQuery.of(context).padding.bottom -
            130,
        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.only(top: 5.0, left: 60),
              child: Row(
                  children: tabs
                      .map(
                        (e) => ElevatedButton(
                          onPressed: () {
                            controller.currentTab = tabs.indexOf(e);
                            controller.update();
                          },
                          child: Text(e),
                          style: ButtonStyle(
                              backgroundColor: MaterialStateProperty.all(
                                  controller.currentTab == tabs.indexOf(e)
                                      ? Colors.lightBlue
                                      : Colors.grey)),
                        ),
                      )
                      .toList()),
            ),
            Expanded(
              child: Container(
                color: Colors.black12,
                padding: EdgeInsets.only(left: 80, top: 20),
                margin: EdgeInsets.only(left: 60, top: 2, right: 60, bottom: 5),
                child: GetBuilder<SettingComponentController>(
                    builder: (controller) {
                  return controller.currentTab == 0
                      ? Column(
                          children: [
                            Row(
                              children: [
                                Text("配置日志路径:"),
                                SizedBox(
                                  width: 10,
                                ),
                                TextButton(
                                  onPressed: () {
                                    Future.microtask(() async {
                                      String? selectedDirectory =
                                          await FilePicker.platform
                                              .getDirectoryPath(
                                                  dialogTitle: "选择日志路径");
                                      if (selectedDirectory != null) {
                                        print("path:$selectedDirectory");
                                        controller.logPath = selectedDirectory;
                                        controller.update();
                                      }
                                    });
                                  },
                                  child: Text(
                                    controller.logPath.isEmpty
                                        ? "尚未设定(不保存)"
                                        : controller.logPath,
                                    style: TextStyle(
                                        decoration: TextDecoration.underline),
                                  ),
                                ),
                              ],
                            ),
                            Row(
                              children: [
                                Text("配置方法路径:"),
                                SizedBox(
                                  width: 10,
                                ),
                                TextButton(
                                  onPressed: () async {
                                    String? selectedDirectory = await FilePicker
                                        .platform
                                        .getDirectoryPath(
                                            dialogTitle: "选择方法路径");
                                    if (selectedDirectory != null) {
                                      print("path:$selectedDirectory");
                                      controller.funcPath = selectedDirectory;
                                      controller.update();
                                    }
                                  },
                                  child: Text(
                                    controller.funcPath.isEmpty
                                        ? "尚未设定(不保存)"
                                        : controller.funcPath,
                                    style: TextStyle(
                                        decoration: TextDecoration.underline),
                                  ),
                                ),
                              ],
                            )
                          ],
                        )
                      : controller.currentTab == 1
                          ? Column(
                              children: [
                                SizedBox(
                                  height: 10,
                                ),
                                Row(
                                  children: [
                                    Container(
                                      width: 320,
                                      height: 230,
                                      padding: EdgeInsets.all(10),
                                      decoration: BoxDecoration(
                                          color: Colors.black12,
                                          borderRadius:
                                              BorderRadius.circular(20)),
                                      child: Column(
                                        children: [
                                          Text(
                                            " ",
                                            style: TextStyle(
                                                fontSize: 20,
                                                fontWeight: FontWeight.bold),
                                          ),
                                          SizedBox(
                                            height: 20,
                                          ),
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            children: [
                                              Text("Kp1"),
                                              NumberInput(onChange: (val) {
                                                controller.inputKp11 =
                                                    val.toInt();
                                                controller.update();
                                                print(
                                                    "val:${controller.inputKp11}");
                                              }),
                                              LoadingButton(
                                                  onPressed:
                                                      controller.inputKp11 == 0
                                                          ? null
                                                          : () async {
                                                              return controller
                                                                  .setTargetTemperature11(
                                                                      controller
                                                                          .inputKp11);
                                                            },
                                                  child: Text("设定")),
                                              Text("${controller.Kp11}(使用中)"),
                                            ],
                                          ),
                                          SizedBox(
                                            height: 10,
                                          ),
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            children: [
                                              Text("Ki1"),
                                              NumberInput(onChange: (val) {
                                                controller.inputKi11 =
                                                    val.toInt();
                                                controller.update();
                                              }),
                                              LoadingButton(
                                                  onPressed:
                                                      controller.inputKi11 == 0
                                                          ? null
                                                          : () async {
                                                              return controller
                                                                  .setAccDuration11(
                                                                      controller
                                                                          .inputKi11);
                                                            },
                                                  child: Text("设定")),
                                              Text("${controller.Ki11}(使用中)"),
                                            ],
                                          ),
                                          SizedBox(
                                            height: 10,
                                          ),
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            children: [
                                              Text("Kd1"),
                                              NumberInput(onChange: (val) {
                                                controller.inputKd11 =
                                                    val.toInt();
                                                controller.update();
                                              }),
                                              LoadingButton(
                                                  onPressed:
                                                      controller.inputKd11 == 0
                                                          ? null
                                                          : () async {
                                                              return controller
                                                                  .setBalanceDuration11(
                                                                      controller
                                                                          .inputKd11);
                                                            },
                                                  child: Text("设定")),
                                              Text("${controller.Kd11}(使用中)"),
                                            ],
                                          ),
                                        ],
                                      ),
                                    ),
                                    Container(
                                      width: 320,
                                      height: 230,
                                      padding: EdgeInsets.all(10),
                                      decoration: BoxDecoration(
                                          color: Colors.black12,
                                          borderRadius:
                                              BorderRadius.circular(20)),
                                      child: Column(
                                        children: [
                                          Text(
                                            "模组1  加热参数",
                                            style: TextStyle(
                                                fontSize: 20,
                                                fontWeight: FontWeight.bold),
                                          ),
                                          SizedBox(
                                            height: 20,
                                          ),
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            children: [
                                              Text("Kp2"),
                                              NumberInput(onChange: (val) {
                                                controller.inputKp12 =
                                                    val.toInt();
                                                controller.update();
                                                print(
                                                    "val:${controller.inputKp12}");
                                              }),
                                              LoadingButton(
                                                  onPressed:
                                                      controller.inputKp12 == 0
                                                          ? null
                                                          : () async {
                                                              return controller
                                                                  .setTargetTemperature12(
                                                                      controller
                                                                          .inputKp12);
                                                            },
                                                  child: Text("设定")),
                                              Text("${controller.Kp12}(使用中)"),
                                            ],
                                          ),
                                          SizedBox(
                                            height: 10,
                                          ),
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            children: [
                                              Text("Ki2"),
                                              NumberInput(onChange: (val) {
                                                controller.inputKi12 =
                                                    val.toInt();
                                                controller.update();
                                              }),
                                              LoadingButton(
                                                  onPressed:
                                                      controller.inputKi12 == 0
                                                          ? null
                                                          : () async {
                                                              return controller
                                                                  .setAccDuration12(
                                                                      controller
                                                                          .inputKi12);
                                                            },
                                                  child: Text("设定")),
                                              Text("${controller.Ki12}(使用中)"),
                                            ],
                                          ),
                                          SizedBox(
                                            height: 10,
                                          ),
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            children: [
                                              Text("Kd2"),
                                              NumberInput(onChange: (val) {
                                                controller.inputKd12 =
                                                    val.toInt();
                                                controller.update();
                                              }),
                                              LoadingButton(
                                                  onPressed:
                                                      controller.inputKd12 == 0
                                                          ? null
                                                          : () async {
                                                              return controller
                                                                  .setBalanceDuration12(
                                                                      controller
                                                                          .inputKd12);
                                                            },
                                                  child: Text("设定")),
                                              Text("${controller.Kd12}(使用中)"),
                                            ],
                                          ),
                                        ],
                                      ),
                                    ),
                                    Container(
                                      width: 320,
                                      height: 230,
                                      padding: EdgeInsets.all(10),
                                      decoration: BoxDecoration(
                                          color: Colors.black12,
                                          borderRadius:
                                              BorderRadius.circular(20)),
                                      child: Column(
                                        children: [
                                          Text(
                                            " ",
                                            style: TextStyle(
                                                fontSize: 20,
                                                fontWeight: FontWeight.bold),
                                          ),
                                          SizedBox(
                                            height: 20,
                                          ),
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            children: [
                                              Text("Kp3"),
                                              NumberInput(onChange: (val) {
                                                controller.inputKp13 =
                                                    val.toInt();
                                                controller.update();
                                                print("val:${controller.Kp13}");
                                              }),
                                              LoadingButton(
                                                  onPressed:
                                                      controller.inputKp13 == 0
                                                          ? null
                                                          : () async {
                                                              return controller
                                                                  .setTargetTemperature13(
                                                                      controller
                                                                          .inputKp13);
                                                            },
                                                  child: Text("设定")),
                                              Text("${controller.Kp13}(使用中)"),
                                            ],
                                          ),
                                          SizedBox(
                                            height: 10,
                                          ),
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            children: [
                                              Text("Ki3"),
                                              NumberInput(onChange: (val) {
                                                controller.inputKi13 =
                                                    val.toInt();
                                                controller.update();
                                              }),
                                              LoadingButton(
                                                  onPressed:
                                                      controller.inputKi13 == 0
                                                          ? null
                                                          : () async {
                                                              return controller
                                                                  .setAccDuration13(
                                                                      controller
                                                                          .inputKi13);
                                                            },
                                                  child: Text("设定")),
                                              Text("${controller.Ki13}(使用中)"),
                                            ],
                                          ),
                                          SizedBox(
                                            height: 10,
                                          ),
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            children: [
                                              Text("Kd3"),
                                              NumberInput(onChange: (val) {
                                                controller.inputKd13 =
                                                    val.toInt();
                                                controller.update();
                                              }),
                                              LoadingButton(
                                                  onPressed:
                                                      controller.inputKd13 == 0
                                                          ? null
                                                          : () async {
                                                              return controller
                                                                  .setBalanceDuration13(
                                                                      controller
                                                                          .inputKd13);
                                                            },
                                                  child: Text("设定")),
                                              Text("${controller.Kd13}(使用中)"),
                                            ],
                                          ),
                                        ],
                                      ),
                                    )
                                  ],
                                ),
                                SizedBox(
                                  height: 40,
                                ),
                                Row(
                                  children: [
                                    Container(
                                      width: 320,
                                      height: 230,
                                      padding: EdgeInsets.all(10),
                                      decoration: BoxDecoration(
                                          color: Colors.black12,
                                          borderRadius:
                                              BorderRadius.circular(20)),
                                      child: Column(
                                        children: [
                                          Text(
                                            " ",
                                            style: TextStyle(
                                                fontSize: 20,
                                                fontWeight: FontWeight.bold),
                                          ),
                                          SizedBox(
                                            height: 20,
                                          ),
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            children: [
                                              Text("Kp1"),
                                              NumberInput(onChange: (val) {
                                                controller.inputKp21 =
                                                    val.toInt();
                                                controller.update();
                                                print(
                                                    "val:${controller.inputKp21}");
                                              }),
                                              LoadingButton(
                                                  onPressed:
                                                      controller.inputKp21 == 0
                                                          ? null
                                                          : () async {
                                                              return controller
                                                                  .setTargetTemperature21(
                                                                      controller
                                                                          .inputKp21);
                                                            },
                                                  child: Text("设定")),
                                              Text("${controller.Kp21}(使用中)"),
                                            ],
                                          ),
                                          SizedBox(
                                            height: 10,
                                          ),
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            children: [
                                              Text("Ki1"),
                                              NumberInput(onChange: (val) {
                                                controller.inputKi21 =
                                                    val.toInt();
                                                controller.update();
                                              }),
                                              LoadingButton(
                                                  onPressed:
                                                      controller.inputKi21 == 0
                                                          ? null
                                                          : () async {
                                                              return controller
                                                                  .setAccDuration21(
                                                                      controller
                                                                          .inputKi21);
                                                            },
                                                  child: Text("设定")),
                                              Text("${controller.Ki21}(使用中)"),
                                            ],
                                          ),
                                          SizedBox(
                                            height: 10,
                                          ),
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            children: [
                                              Text("Kd1"),
                                              NumberInput(onChange: (val) {
                                                controller.inputKd21 =
                                                    val.toInt();
                                                controller.update();
                                              }),
                                              LoadingButton(
                                                  onPressed:
                                                      controller.inputKd21 == 0
                                                          ? null
                                                          : () async {
                                                              return controller
                                                                  .setBalanceDuration21(
                                                                      controller
                                                                          .inputKd21);
                                                            },
                                                  child: Text("设定")),
                                              Text("${controller.Kd21}(使用中)"),
                                            ],
                                          ),
                                        ],
                                      ),
                                    ),
                                    Container(
                                      width: 320,
                                      height: 230,
                                      padding: EdgeInsets.all(10),
                                      decoration: BoxDecoration(
                                          color: Colors.black12,
                                          borderRadius:
                                              BorderRadius.circular(20)),
                                      child: Column(
                                        children: [
                                          Text(
                                            "模组2  加热参数",
                                            style: TextStyle(
                                                fontSize: 20,
                                                fontWeight: FontWeight.bold),
                                          ),
                                          SizedBox(
                                            height: 20,
                                          ),
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            children: [
                                              Text("Kp2"),
                                              NumberInput(onChange: (val) {
                                                controller.inputKp22 =
                                                    val.toInt();
                                                controller.update();
                                                print(
                                                    "val:${controller.inputKp22}");
                                              }),
                                              LoadingButton(
                                                  onPressed:
                                                      controller.inputKp22 == 0
                                                          ? null
                                                          : () async {
                                                              return controller
                                                                  .setTargetTemperature22(
                                                                      controller
                                                                          .inputKp22);
                                                            },
                                                  child: Text("设定")),
                                              Text("${controller.Kp22}(使用中)"),
                                            ],
                                          ),
                                          SizedBox(
                                            height: 10,
                                          ),
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            children: [
                                              Text("Ki2"),
                                              NumberInput(onChange: (val) {
                                                controller.inputKi22 =
                                                    val.toInt();
                                                controller.update();
                                              }),
                                              LoadingButton(
                                                  onPressed:
                                                      controller.inputKi22 == 0
                                                          ? null
                                                          : () async {
                                                              return controller
                                                                  .setAccDuration22(
                                                                      controller
                                                                          .inputKi22);
                                                            },
                                                  child: Text("设定")),
                                              Text("${controller.Ki22}(使用中)"),
                                            ],
                                          ),
                                          SizedBox(
                                            height: 10,
                                          ),
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            children: [
                                              Text("Kd2"),
                                              NumberInput(onChange: (val) {
                                                controller.inputKd22 =
                                                    val.toInt();
                                                controller.update();
                                              }),
                                              LoadingButton(
                                                  onPressed:
                                                      controller.inputKd22 == 0
                                                          ? null
                                                          : () async {
                                                              return controller
                                                                  .setBalanceDuration22(
                                                                      controller
                                                                          .inputKd22);
                                                            },
                                                  child: Text("设定")),
                                              Text("${controller.Kd22}(使用中)"),
                                            ],
                                          ),
                                        ],
                                      ),
                                    ),
                                    Container(
                                      width: 320,
                                      height: 230,
                                      padding: EdgeInsets.all(10),
                                      decoration: BoxDecoration(
                                          color: Colors.black12,
                                          borderRadius:
                                              BorderRadius.circular(20)),
                                      child: Column(
                                        children: [
                                          Text(
                                            " ",
                                            style: TextStyle(
                                                fontSize: 20,
                                                fontWeight: FontWeight.bold),
                                          ),
                                          SizedBox(
                                            height: 20,
                                          ),
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            children: [
                                              Text("Kp3"),
                                              NumberInput(onChange: (val) {
                                                controller.inputKp23 =
                                                    val.toInt();
                                                controller.update();
                                                print(
                                                    "val:${controller.inputKp23}");
                                              }),
                                              LoadingButton(
                                                  onPressed:
                                                      controller.inputKp23 == 0
                                                          ? null
                                                          : () async {
                                                              return controller
                                                                  .setTargetTemperature23(
                                                                      controller
                                                                          .inputKp23);
                                                            },
                                                  child: Text("设定")),
                                              Text("${controller.Kp23}(使用中)"),
                                            ],
                                          ),
                                          SizedBox(
                                            height: 10,
                                          ),
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            children: [
                                              Text("Ki3"),
                                              NumberInput(onChange: (val) {
                                                controller.inputKi23 =
                                                    val.toInt();
                                                controller.update();
                                              }),
                                              LoadingButton(
                                                  onPressed:
                                                      controller.inputKi23 == 0
                                                          ? null
                                                          : () async {
                                                              return controller
                                                                  .setAccDuration23(
                                                                      controller
                                                                          .inputKi23);
                                                            },
                                                  child: Text("设定")),
                                              Text("${controller.Ki23}(使用中)"),
                                            ],
                                          ),
                                          SizedBox(
                                            height: 10,
                                          ),
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            children: [
                                              Text("Kd3"),
                                              NumberInput(onChange: (val) {
                                                controller.inputKd23 =
                                                    val.toInt();
                                                controller.update();
                                              }),
                                              LoadingButton(
                                                  onPressed:
                                                      controller.inputKd23 == 0
                                                          ? null
                                                          : () async {
                                                              return controller
                                                                  .setBalanceDuration23(
                                                                      controller
                                                                          .inputKd23);
                                                            },
                                                  child: Text("设定")),
                                              Text("${controller.Kd23}(使用中)"),
                                            ],
                                          ),
                                        ],
                                      ),
                                    )
                                  ],
                                ),
                              ],
                            )
                          : Stack(
                              children: [
                                TextButton(
                                    onPressed: () {
                                      controller.addUserInfo.clear();
                                      controller.addUserInfo["privilege"] =
                                          UserPrivilege.userExecutor;
                                      final List<UserPrivilege> showPrivileges =
                                          [];
                                      for (var i
                                          in controller.privilegeMap.keys) {
                                        if (controller.deviceService
                                                    .currentUser !=
                                                null &&
                                            i.index >=
                                                controller
                                                    .deviceService
                                                    .currentUser!
                                                    .privilege
                                                    .index) {
                                          continue;
                                        }
                                        showPrivileges.add(i);
                                      }
                                      showBaseDialog(
                                        "新增用户",
                                        Size(500, 350),
                                        GetBuilder<SettingComponentController>(
                                          builder: (controller) {
                                            return Stack(
                                              children: [
                                                Column(
                                                  children: [
                                                    Row(
                                                      mainAxisAlignment:
                                                          MainAxisAlignment
                                                              .spaceBetween,
                                                      children: [
                                                        Text("名称"),
                                                        SizedBox(
                                                          height: 44,
                                                          width: 150,
                                                          child: TextField(
                                                            maxLines: 1,
                                                            textAlign: TextAlign
                                                                .center,
                                                            style: TextStyle(
                                                                textBaseline:
                                                                    TextBaseline
                                                                        .alphabetic),
                                                            decoration:
                                                                InputDecoration(
                                                              hintStyle: TextStyle(
                                                                  color: Color
                                                                      .fromRGBO(
                                                                          153,
                                                                          151,
                                                                          151,
                                                                          0.3),
                                                                  fontSize: 16),
                                                              hintText: "输入名称",
                                                              // border: OutlineInputBorder(),
                                                              alignLabelWithHint:
                                                                  true,
                                                              contentPadding:
                                                                  EdgeInsets.only(
                                                                      top: 0,
                                                                      bottom:
                                                                          0),
                                                            ),
                                                            onChanged: (val) {
                                                              controller
                                                                      .addUserInfo[
                                                                  "name"] = val;
                                                            },
                                                          ),
                                                        )
                                                      ],
                                                    ),
                                                    Row(
                                                      mainAxisAlignment:
                                                          MainAxisAlignment
                                                              .spaceBetween,
                                                      children: [
                                                        Text("账号"),
                                                        SizedBox(
                                                          height: 44,
                                                          width: 150,
                                                          child: TextField(
                                                            maxLines: 1,
                                                            textAlign: TextAlign
                                                                .center,
                                                            style: TextStyle(
                                                                textBaseline:
                                                                    TextBaseline
                                                                        .alphabetic),
                                                            decoration:
                                                                InputDecoration(
                                                              hintStyle: TextStyle(
                                                                  color: Color
                                                                      .fromRGBO(
                                                                          153,
                                                                          151,
                                                                          151,
                                                                          0.3),
                                                                  fontSize: 16),
                                                              hintText: "输入账号",
                                                              // border: OutlineInputBorder(),
                                                              alignLabelWithHint:
                                                                  true,
                                                              contentPadding:
                                                                  EdgeInsets.only(
                                                                      top: 0,
                                                                      bottom:
                                                                          0),
                                                            ),
                                                            onChanged: (val) {
                                                              controller
                                                                      .addUserInfo[
                                                                  "id"] = val;
                                                            },
                                                          ),
                                                        )
                                                      ],
                                                    ),
                                                    Row(
                                                      mainAxisAlignment:
                                                          MainAxisAlignment
                                                              .spaceBetween,
                                                      children: [
                                                        Text("密码"),
                                                        SizedBox(
                                                          height: 44,
                                                          width: 150,
                                                          child: TextField(
                                                            maxLines: 1,
                                                            textAlign: TextAlign
                                                                .center,
                                                            style: TextStyle(
                                                                textBaseline:
                                                                    TextBaseline
                                                                        .alphabetic),
                                                            decoration:
                                                                InputDecoration(
                                                              hintStyle: TextStyle(
                                                                  color: Color
                                                                      .fromRGBO(
                                                                          153,
                                                                          151,
                                                                          151,
                                                                          0.3),
                                                                  fontSize: 16),
                                                              hintText: "输入密码",
                                                              // border: OutlineInputBorder(),
                                                              alignLabelWithHint:
                                                                  true,
                                                              contentPadding:
                                                                  EdgeInsets.only(
                                                                      top: 0,
                                                                      bottom:
                                                                          0),
                                                            ),
                                                            obscureText: true,
                                                            onChanged: (val) {
                                                              controller.addUserInfo[
                                                                      "password"] =
                                                                  val.toString();
                                                            },
                                                          ),
                                                        )
                                                      ],
                                                    ),
                                                    Row(
                                                      mainAxisAlignment:
                                                          MainAxisAlignment
                                                              .spaceBetween,
                                                      children: [
                                                        Text("重复密码"),
                                                        SizedBox(
                                                          height: 44,
                                                          width: 150,
                                                          child: TextField(
                                                            maxLines: 1,
                                                            textAlign: TextAlign
                                                                .center,
                                                            obscureText: true,
                                                            style: TextStyle(
                                                                textBaseline:
                                                                    TextBaseline
                                                                        .alphabetic),
                                                            decoration:
                                                                InputDecoration(
                                                              hintStyle: TextStyle(
                                                                  color: Color
                                                                      .fromRGBO(
                                                                          153,
                                                                          151,
                                                                          151,
                                                                          0.3),
                                                                  fontSize: 16),
                                                              hintText:
                                                                  "输入重复密码",
                                                              // border: OutlineInputBorder(),
                                                              alignLabelWithHint:
                                                                  true,
                                                              contentPadding:
                                                                  EdgeInsets.only(
                                                                      top: 0,
                                                                      bottom:
                                                                          0),
                                                            ),
                                                            keyboardType:
                                                                TextInputType
                                                                    .visiblePassword,
                                                            onChanged: (val) {
                                                              controller.addUserInfo[
                                                                      "rePassword"] =
                                                                  val.toString();
                                                            },
                                                          ),
                                                        )
                                                      ],
                                                    ),
                                                    Row(
                                                      mainAxisAlignment:
                                                          MainAxisAlignment
                                                              .spaceBetween,
                                                      children: [
                                                        Text("权限"),
                                                        SizedBox(
                                                          width: 150,
                                                          height: 50,
                                                          child: DropdownButton(
                                                            value: controller
                                                                        .addUserInfo[
                                                                    "privilege"]
                                                                as UserPrivilege,
                                                            items:
                                                                showPrivileges
                                                                    .map((e) =>
                                                                        DropdownMenuItem<
                                                                            UserPrivilege>(
                                                                          value:
                                                                              e,
                                                                          child:
                                                                              Text(controller.privilegeMap[e]!),
                                                                        ))
                                                                    .toList(),
                                                            onChanged:
                                                                (UserPrivilege?
                                                                    val) {
                                                              controller
                                                                      .addUserInfo[
                                                                  "privilege"] = val;
                                                              controller
                                                                  .update();
                                                            },
                                                            isExpanded: true,
                                                            hint: Text(
                                                              "无配置",
                                                              style: TextStyle(
                                                                  color: Colors
                                                                      .black38),
                                                            ),
                                                            alignment: Alignment
                                                                .center,
                                                          ),
                                                        )
                                                      ],
                                                    ),
                                                  ],
                                                ),
                                                Positioned(
                                                  right: 60,
                                                  bottom: 0,
                                                  child: ElevatedButton(
                                                    onPressed: () {
                                                      print(
                                                          "--${controller.addUserInfo['password']}");
                                                      if (controller
                                                                  .addUserInfo[
                                                              'password'] !=
                                                          controller
                                                                  .addUserInfo[
                                                              'rePassword']) {
                                                        showHint("两次密码不一致",
                                                            HintType.WARNING);
                                                        return;
                                                      }
                                                      if (controller
                                                          .addUserInfo[
                                                              'password']
                                                          .contains(" ")) {
                                                        showHint("密码不能包含空格!",
                                                            HintType.WARNING);
                                                        return;
                                                      }
                                                      if (controller
                                                              .addUserInfo[
                                                                  'password']
                                                              .length <
                                                          6) {
                                                        showHint("密码长度不能少于6位!",
                                                            HintType.WARNING);
                                                        return;
                                                      }
                                                      final r = controller
                                                          .saveUser(controller
                                                              .addUserInfo);
                                                      if (r) {
                                                        Navigator.pop(context);
                                                        return;
                                                      }
                                                    },
                                                    child: Text("确认"),
                                                  ),
                                                )
                                              ],
                                            );
                                          },
                                        ),
                                      );
                                    },
                                    child: Text("+ 新增")),
                                Center(
                                  child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.center,
                                    children: [
                                      Text(
                                        "用户设置",
                                        style: TextStyle(
                                            fontSize: 22,
                                            fontWeight: FontWeight.bold),
                                      ),
                                      SizedBox(
                                        height: 20,
                                      ),
                                      SizedBox(
                                        height: 30,
                                        child: Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.spaceBetween,
                                          children: [
                                            Row(
                                              children: [
                                                Text(
                                                  "当前用户",
                                                  style: TextStyle(
                                                      fontWeight:
                                                          FontWeight.bold,
                                                      fontSize: 18),
                                                ),
                                                SizedBox(
                                                  width: 10,
                                                ),
                                                Text(controller.deviceService
                                                            .currentUser ==
                                                        null
                                                    ? "无"
                                                    : controller.deviceService
                                                        .currentUser!.name),
                                              ],
                                            ),
                                            Row(
                                              children: [
                                                Text(
                                                  "账号",
                                                  style: TextStyle(
                                                      fontWeight:
                                                          FontWeight.bold,
                                                      fontSize: 18),
                                                ),
                                                SizedBox(
                                                  width: 10,
                                                ),
                                                Text(controller.deviceService
                                                            .currentUser ==
                                                        null
                                                    ? "无"
                                                    : controller.deviceService
                                                        .currentUser!.id),
                                              ],
                                            ),
                                            Row(
                                              children: [
                                                Text(
                                                  "权限",
                                                  style: TextStyle(
                                                      fontWeight:
                                                          FontWeight.bold,
                                                      fontSize: 18),
                                                ),
                                                SizedBox(
                                                  width: 10,
                                                ),
                                                Text(controller.deviceService
                                                            .currentUser ==
                                                        null
                                                    ? "无"
                                                    : controller.privilegeMap[
                                                        controller
                                                            .deviceService
                                                            .currentUser!
                                                            .privilege]!),
                                              ],
                                            ),
                                            TextButton(
                                                onPressed: () {
                                                  showHint("火速开发中",
                                                      HintType.WARNING);
                                                },
                                                child: Text("编辑")),
                                          ],
                                        ),
                                      ),
                                      SizedBox(
                                        height: 20,
                                      ),
                                      Expanded(
                                        child: controller.userList.isEmpty
                                            ? Center(child: Text("无数据"))
                                            : Container(
                                                // color: Colors.green,
                                                child: Column(
                                                  children: [
                                                    Container(
                                                      child: Row(
                                                        children: [
                                                          Expanded(
                                                            child: Center(
                                                                child:
                                                                    Text("名称")),
                                                            flex: 2,
                                                          ),
                                                          Expanded(
                                                            child: Center(
                                                                child: Text(
                                                                    "创建时间")),
                                                            flex: 3,
                                                          ),
                                                          Expanded(
                                                            child: Center(
                                                                child:
                                                                    Text("状态")),
                                                            flex: 2,
                                                          ),
                                                          Expanded(
                                                              child: Center(
                                                                  child: Text(
                                                                      "权限")),
                                                              flex: 2),
                                                          Expanded(
                                                            child: Center(
                                                                child: Text(
                                                                    "创建者")),
                                                            flex: 2,
                                                          ),
                                                          Expanded(
                                                            child: Center(
                                                                child:
                                                                    Text("编辑")),
                                                            flex: 2,
                                                          ),
                                                        ],
                                                      ),
                                                      height: 30,
                                                      color: Colors.white10
                                                          .withOpacity(0.5),
                                                    ),
                                                    Expanded(
                                                      child: ListView.separated(
                                                          separatorBuilder:
                                                              (BuildContext
                                                                      context,
                                                                  int index) {
                                                            return const Divider(
                                                                height: 1.0,
                                                                color: Colors
                                                                    .black12);
                                                          },
                                                          itemCount: controller
                                                              .userList.length,
                                                          itemBuilder:
                                                              (context, index) {
                                                            final createdUser = controller
                                                                .userList
                                                                .firstWhereOrNull((element) =>
                                                                    element
                                                                        .id ==
                                                                    controller
                                                                        .userList[
                                                                            index]
                                                                        .createdBy);
                                                            final createdUserName =
                                                                createdUser ==
                                                                        null
                                                                    ? "-"
                                                                    : createdUser
                                                                        .name;
                                                            return Row(
                                                              mainAxisAlignment:
                                                                  MainAxisAlignment
                                                                      .spaceEvenly,
                                                              children: [
                                                                Expanded(
                                                                    flex: 2,
                                                                    child:
                                                                        Center(
                                                                      child: Text(controller
                                                                          .userList[
                                                                              index]
                                                                          .name),
                                                                    )),
                                                                Expanded(
                                                                  flex: 3,
                                                                  child: Center(
                                                                    child: Text(DateTime.fromMillisecondsSinceEpoch(controller.userList[index].createdAt *
                                                                            1000)
                                                                        .toString()
                                                                        .substring(
                                                                            0,
                                                                            19)),
                                                                  ),
                                                                ),
                                                                Expanded(
                                                                  child: Center(
                                                                    child: controller
                                                                            .userList[index]
                                                                            .enable
                                                                        ? Icon(
                                                                            Icons.check_circle,
                                                                            color:
                                                                                Colors.green,
                                                                          )
                                                                        : Icon(
                                                                            Icons.lock,
                                                                            color:
                                                                                Colors.grey,
                                                                          ),
                                                                  ),
                                                                  flex: 2,
                                                                ),
                                                                Expanded(
                                                                  child: Center(
                                                                    child: Text(controller
                                                                            .privilegeMap[
                                                                        controller
                                                                            .userList[index]
                                                                            .privilege]!),
                                                                  ),
                                                                  flex: 2,
                                                                ),
                                                                Expanded(
                                                                    flex: 2,
                                                                    child: Center(
                                                                        child: Text(
                                                                            createdUserName))),
                                                                Expanded(
                                                                  flex: 1,
                                                                  child: Center(
                                                                    child: TextButton(
                                                                        onPressed: (controller.deviceService.currentUser != null && controller.deviceService.currentUser!.privilege.index < controller.userList[index].privilege.index)
                                                                            ? null
                                                                            : () {
                                                                                controller.editUserInfo = controller.userList[index].toJson();
                                                                                showBaseDialog(
                                                                                  "修改用户",
                                                                                  Size(500, 350),
                                                                                  GetBuilder<SettingComponentController>(
                                                                                    builder: (controller) {
                                                                                      return Stack(
                                                                                        children: [
                                                                                          Column(
                                                                                            children: [
                                                                                              Row(
                                                                                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                                                                                children: [
                                                                                                  Text("名称"),
                                                                                                  SizedBox(
                                                                                                    height: 44,
                                                                                                    width: 150,
                                                                                                    child: TextField(
                                                                                                      maxLines: 1,
                                                                                                      textAlign: TextAlign.center,
                                                                                                      style: TextStyle(textBaseline: TextBaseline.alphabetic),
                                                                                                      decoration: InputDecoration(
                                                                                                        hintStyle: TextStyle(color: Color.fromRGBO(153, 151, 151, 0.3), fontSize: 16),
                                                                                                        hintText: "输入名称",
                                                                                                        // border: OutlineInputBorder(),
                                                                                                        alignLabelWithHint: true,
                                                                                                        contentPadding: EdgeInsets.only(top: 0, bottom: 0),
                                                                                                      ),
                                                                                                      onChanged: (val) {
                                                                                                        controller.editUserInfo["name"] = val;
                                                                                                      },
                                                                                                    ),
                                                                                                  )
                                                                                                ],
                                                                                              ),
                                                                                              Row(
                                                                                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                                                                                children: [
                                                                                                  Text("账号"),
                                                                                                  SizedBox(
                                                                                                    height: 44,
                                                                                                    width: 150,
                                                                                                    child: TextField(
                                                                                                      maxLines: 1,
                                                                                                      textAlign: TextAlign.center,
                                                                                                      style: TextStyle(textBaseline: TextBaseline.alphabetic),
                                                                                                      decoration: InputDecoration(
                                                                                                        hintStyle: TextStyle(color: Color.fromRGBO(153, 151, 151, 0.3), fontSize: 16),
                                                                                                        hintText: "输入账号",
                                                                                                        // border: OutlineInputBorder(),
                                                                                                        alignLabelWithHint: true,
                                                                                                        contentPadding: EdgeInsets.only(top: 0, bottom: 0),
                                                                                                      ),
                                                                                                      onChanged: (val) {
                                                                                                        controller.editUserInfo["id"] = val;
                                                                                                      },
                                                                                                    ),
                                                                                                  )
                                                                                                ],
                                                                                              ),
                                                                                              Row(
                                                                                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                                                                                children: [
                                                                                                  Text("密码"),
                                                                                                  SizedBox(
                                                                                                    height: 44,
                                                                                                    width: 150,
                                                                                                    child: TextField(
                                                                                                      maxLines: 1,
                                                                                                      textAlign: TextAlign.center,
                                                                                                      style: TextStyle(textBaseline: TextBaseline.alphabetic),
                                                                                                      decoration: InputDecoration(
                                                                                                        hintStyle: TextStyle(color: Color.fromRGBO(153, 151, 151, 0.3), fontSize: 16),
                                                                                                        hintText: "输入密码",
                                                                                                        // border: OutlineInputBorder(),
                                                                                                        alignLabelWithHint: true,
                                                                                                        contentPadding: EdgeInsets.only(top: 0, bottom: 0),
                                                                                                      ),
                                                                                                      obscureText: true,
                                                                                                      onChanged: (val) {
                                                                                                        controller.editUserInfo["password"] = val.toString();
                                                                                                      },
                                                                                                    ),
                                                                                                  )
                                                                                                ],
                                                                                              ),
                                                                                              Row(
                                                                                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                                                                                children: [
                                                                                                  Text("重复密码"),
                                                                                                  SizedBox(
                                                                                                    height: 44,
                                                                                                    width: 150,
                                                                                                    child: TextField(
                                                                                                      maxLines: 1,
                                                                                                      textAlign: TextAlign.center,
                                                                                                      obscureText: true,
                                                                                                      style: TextStyle(textBaseline: TextBaseline.alphabetic),
                                                                                                      decoration: InputDecoration(
                                                                                                        hintStyle: TextStyle(color: Color.fromRGBO(153, 151, 151, 0.3), fontSize: 16),
                                                                                                        hintText: "输入重复密码",
                                                                                                        // border: OutlineInputBorder(),
                                                                                                        alignLabelWithHint: true,
                                                                                                        contentPadding: EdgeInsets.only(top: 0, bottom: 0),
                                                                                                      ),
                                                                                                      keyboardType: TextInputType.visiblePassword,
                                                                                                      onChanged: (val) {
                                                                                                        controller.editUserInfo["rePassword"] = val.toString();
                                                                                                      },
                                                                                                    ),
                                                                                                  )
                                                                                                ],
                                                                                              ),
                                                                                              Row(
                                                                                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                                                                                children: [
                                                                                                  Text("权限"),
                                                                                                  SizedBox(
                                                                                                    width: 150,
                                                                                                    height: 50,
                                                                                                    child: DropdownButton(
                                                                                                      value: UserPrivilege.values[controller.editUserInfo["privilege"]],
                                                                                                      items: controller.privilegeMap.keys
                                                                                                          .map((e) => DropdownMenuItem<UserPrivilege>(
                                                                                                                value: e,
                                                                                                                child: Text(controller.privilegeMap[e]!),
                                                                                                              ))
                                                                                                          .toList(),
                                                                                                      onChanged: (UserPrivilege? val) {
                                                                                                        controller.editUserInfo["privilege"] = val?.index;
                                                                                                        controller.update();
                                                                                                      },
                                                                                                      isExpanded: true,
                                                                                                      hint: Text(
                                                                                                        "无配置",
                                                                                                        style: TextStyle(color: Colors.black38),
                                                                                                      ),
                                                                                                      alignment: Alignment.center,
                                                                                                    ),
                                                                                                  )
                                                                                                ],
                                                                                              ),
                                                                                            ],
                                                                                          ),
                                                                                          Positioned(
                                                                                            right: 60,
                                                                                            bottom: 0,
                                                                                            child: ElevatedButton(
                                                                                              onPressed: () {
                                                                                                print("--${controller.editUserInfo['password']}");
                                                                                                if (controller.editUserInfo['password'] != controller.editUserInfo['rePassword']) {
                                                                                                  showHint("两次密码不一致", HintType.WARNING);
                                                                                                  return;
                                                                                                }
                                                                                                if (controller.editUserInfo['password'].contains(" ")) {
                                                                                                  showHint("密码不能包含空格!", HintType.WARNING);
                                                                                                  return;
                                                                                                }
                                                                                                if (controller.editUserInfo['password'].length < 6) {
                                                                                                  showHint("密码长度不能少于6位!", HintType.WARNING);
                                                                                                  return;
                                                                                                }
                                                                                                // 修改逻辑：先删除旧用户，再添加新用户
                                                                                                // 先删除旧用户
                                                                                                controller.delUser(controller.userList[index].id, controller.userList[index].name);

                                                                                                // 然后保存新用户（添加）
                                                                                                final r = controller.saveUser(controller.editUserInfo);
                                                                                                if (r) {
                                                                                                  Navigator.pop(context);
                                                                                                  return;
                                                                                                }
                                                                                              },
                                                                                              child: Text("确认"),
                                                                                            ),
                                                                                          )
                                                                                        ],
                                                                                      );
                                                                                    },
                                                                                  ),
                                                                                );
                                                                              },
                                                                        child: Text("修改")),
                                                                  ),
                                                                ),
                                                                Expanded(
                                                                  flex: 1,
                                                                  child: Center(
                                                                    child: IconButton(
                                                                        onPressed: controller.deviceService.currentUser != null && controller.deviceService.currentUser!.privilege.index <= controller.userList[index].privilege.index
                                                                            ? null
                                                                            : () async {
                                                                                // 确认弹窗
                                                                                if (!await showConfirmDialog("确认修改用户${controller.userList[index].name}?")) {
                                                                                  return;
                                                                                }

                                                                                controller.delUser(controller.userList[index].id, controller.userList[index].name);
                                                                              },
                                                                        icon: Icon(Icons.delete_outlined)),
                                                                  ),
                                                                ),
                                                              ],
                                                            );
                                                          }),
                                                    ),
                                                  ],
                                                ),
                                              ),
                                      )
                                    ],
                                  ),
                                ),
                              ],
                            );
                }),
              ),
            ),
            Text(
              "应用版本: v${controller.appVersion}",
              style: TextStyle(color: Colors.grey, fontSize: 20),
            ),
          ],
        ),
      );
    });
  }
}
